package com.qvtu.poetry.controller;

import com.qvtu.poetry.entry.Result;
import com.qvtu.poetry.utils.EmailApi;
import com.qvtu.poetry.utils.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * 验证码码控制器，手机验证码，邮箱验证码
 */
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    @Resource
    EmailApi emailApi;
    @Resource
    private RedisUtil redisUtil; // 注入 Redis 工具类

    //     邮箱的正则表达式
    private static final String EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";

    // 检查邮箱格式的方法
    public static boolean isValidFormat(String email) {
        Pattern pattern = Pattern.compile(EMAIL_REGEX);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
        // 如果匹配成功，返回true
    }

    @RequestMapping("/getCaptchaByEmail")
    public Result<String> requestVerificationCode(@RequestParam String email) {
        // 判断email是否非法
        if (!isValidFormat(email)) {
            return Result.error("邮箱格式错误");
        }
        // 1.生成4位数字验证码
        String verificationCode = generateVerificationCode();
        // 2.发送验证码
        emailApi.sendHtmlEmail("智韵工坊", getStringByVerificationCode(verificationCode), email);
        // 3.将验证码存储到 Redis，设置过期时间为5分钟
        redisUtil.setVerificationCode("email:code:" + email, verificationCode, 60 * 5);
        // 4.返回验证码（实际项目中可以返回一个成功消息，验证码通过其他方式发送给用户）
        System.out.println("验证码已发送，验证码为：" + verificationCode);
        return Result.success("验证码已发送");
    }

    /**
     * 验证码格式
     *
     * @param verificationCode
     * @return
     */
    public String getStringByVerificationCode(String verificationCode) {
        return "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>智韵工坊验证码</title>\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: Arial, sans-serif;\n" +
                "            background-color: #f4f4f4;\n" +
                "            margin: 0;\n" +
                "            padding: 0;\n" +
                "        }\n" +
                "        .container {\n" +
                "            max-width: 600px;\n" +
                "            margin: 20px auto;\n" +
                "            padding: 20px;\n" +
                "            background: #fff;\n" +
                "            border-radius: 8px;\n" +
                "            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n" +
                "            text-align: center;\n" +
                "        }\n" +
                "        .header {\n" +
                "            background-color: #007bff;\n" +
                "            color: #fff;\n" +
                "            padding: 10px;\n" +
                "            border-radius: 8px 8px 0 0;\n" +
                "        }\n" +
                "        .captcha {\n" +
                "            font-size: 24px;\n" +
                "            font-weight: bold;\n" +
                "            margin: 20px 0;\n" +
                "            color: #333;\n" +
                "        }\n" +
                "        .instructions {\n" +
                "            font-size: 16px;\n" +
                "            color: #555;\n" +
                "            margin: 10px 0;\n" +
                "        }\n" +
                "        .button {\n" +
                "            display: inline-block;\n" +
                "            padding: 10px 20px;\n" +
                "            background-color: #007bff;\n" +
                "            color: #fff;\n" +
                "            text-decoration: none;\n" +
                "            border-radius: 4px;\n" +
                "            margin: 20px 0;\n" +
                "        }\n" +
                "        .button:hover {\n" +
                "            background-color: #0056b3;\n" +
                "        }\n" +
                "        .footer {\n" +
                "            font-size: 12px;\n" +
                "            color: #888;\n" +
                "            margin-top: 20px;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\">\n" +
                "        <div class=\"header\">\n" +
                "            <h1>智韵工坊验证码</h1>\n" +
                "        </div>\n" +
                "        <div class=\"content\">\n" +
                "            <div class=\"captcha\" id=\"captcha\">您的验证码是：<span id=\"captchaValue\">" + verificationCode + "</span></div>\n" +
                "            <div class=\"instructions\">请将上述验证码输入到相关页面以完成验证。</div>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";
    }

//    @RequestMapping("/getCaptchaByPhone")


//        // 1.生成4位数字验证码
//        String verificationCode = generateVerificationCode();
//        // 2.发送验证码
//        smsComponent.sendMsg(verificationCode, phone);
//        // 3.将验证码存储到 Redis，设置过期时间为5分钟
//        redisUtil.setVerificationCode("phone:code:" + phone, verificationCode, 60 * 5);
//        // 4.返回验证码（实际项目中可以返回一个成功消息，验证码通过其他方式发送给用户）
//        System.out.println("验证码已发送，验证码为：" + verificationCode);
//        return Result.success("验证码已发送");
//    }

    /**
     * 生成4位数字验证码
     *
     * @return 验证码字符串
     */
    private String generateVerificationCode() {
        Random random = new Random();
        return String.format("%04d", random.nextInt(10000)); // 生成0000到9999的随机数
    }
}